﻿using System;
using System.Collections.Generic;
using System.Linq;
using HiStaff.Base;
using HiStaff.Control;
using HiStaff.Domain.IN;
using HiStaff.Domain.Sys;
using HiStaff.Util;
using HiStaff.Dal;
using Janus.Windows.GridEX;
using System.Data;
using System.Windows.Forms;

namespace HiStaff.iInsurance
{
    public partial class InsuranceChange : FormBase
    {
        public InsuranceChange()
        {
            InitializeComponent();
        }
        protected override bool FormLoad()
        {
            try
            {
                AddBasicCommandBar();
                SetToolStripButtonBar(CommandBarManager.ActionCommand.Cancel);
                BaseStatus = Util.Enum.FormStatus.View;
                SetStatusInputControl(this);
                return true;
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
                return false;
            }
        }

        private void BindData()
        {
            try
            {
                var lstEmployee = OracleHelper.ExcuteSelectMultiObject<IN_CHANGE>("APRS_IN_CHANGE", new SEARCH { TERMINATE  = chkTerminate.Value , ORGID = trvOrg.OrgId, CHGFROM = dtFromdate.Value, CHGTO = dtTodate.Value });
                grdMain.DataSource = lstEmployee;
                grdMain.Refresh();
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
            }
        }
        private void trvOrg_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
        {
            try
            {
                BindData();
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
            }
        }

       
        protected override bool SaveButtonClick()
        {
            try
            {


                var objSave = new IN_CHANGE();
                ValueControlsToObject(objSave, this);
                //objSave.EMPLOYEEID = ConvertHelper.ToDecimal(hiEmployeeSelection1.DataFieldID );
                if (!ChkThoaithu.Checked && !ChkThoaithu.Checked)
                {
                    objSave.CLT = null;
                }
                else
                {
                    if (chkTruyThu.Checked == true)
                    {
                        objSave.CLT = 0;
                    }
                    else
                    {
                        objSave.CLT = 1;
                    }
                    
                    if (BaseStatus == Util.Enum.FormStatus.Add)
                    {
                        objSave.ID = 0;
                    }
                }

                OracleHelper.ExcuteNonQuery("APRU_IN_CHANGE", objSave);


                BindData();
                MessageBox.Show(GetMessageById(Constant.MSG_TRANSACTION_SUCCESSFUL), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                return true;
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
                return false;
            }
        }

        protected override bool CancelButtonClick()
        {
            try
            {
                BindData();
                return true;
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
                return false;
            }
        }

        protected override bool DeleteButtonClick()
        {
            if (grdMain.SelectedItems.Count > 0)
            {
                var row = grdMain.SelectedItems[0];
                if (row.RowType == Janus.Windows.GridEX.RowType.Record)
                {
                    if (OracleHelper.ExcuteNonQuery("APRD_IN_CHANGE", row.GetRow().DataRow))
                    {
                        MessageBox.Show(GetMessageById(Constant.MSG_TRANSACTION_SUCCESSFUL), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                        BindData();
                        return true;
                    }
                }
            }
            return false;
        }
        private void BindDataFromGrid()
        {
            BindDataFromGridToForm(grdMain, this);
        }

        private void grdMain_SelectionChanged(object sender, EventArgs e)
        {
            BindDataFromGrid();
        }
        private void Get_Collect()
        {
            System.DateTime d = new DateTime(((DateTime)cdPeriodDate.Value).Year, ((DateTime)cdPeriodDate.Value).Month, 1);
             
            if ((cdPeriodDate.Value != null) && (cdFromMonth.Value != null))
            {
                if (d > cdFromMonth.Value)
                {
                    cdFromClt.Value = cdFromMonth.Value;
                   
                    cdToClt.Value =  d.AddMonths(-1);


                    var lstCollect = OracleHelper.ExcuteSelectMultiObject<IN_CHANGE>("PRS_COLLECT", new IN_CHANGE { CLTFRMMONTH = cdFromClt.Value, CLTTOMONTH = d, OLD_SALARY =  ConvertHelper.ToDecimal(txtOldSalary.Text) , NEW_SALARY = ConvertHelper.ToDecimal(txtNewSalary.Text) , SO = (chkSo.Checked ? 1 : 0), HE = (chkHe.Checked ? 1 : 0), UE = (chkUe.Checked ? 1 : 0) });
                       txtCltSo.Value = lstCollect[0].CLTSO;
                       txtCltHe.Value = lstCollect[0].CLTHE;
                       txtCltUe.Value = lstCollect[0].CLTUE;
                       if (lstCollect[0].CLT == 0)
                       {
                           chkTruyThu.Checked = true ;
                           ChkThoaithu.Checked = false ;
                       }
                       else if (lstCollect[0].CLT == 1)
                       { 
                           chkTruyThu.Checked = false ;
                           ChkThoaithu.Checked = true  ;
                       }else
                       {
                           chkTruyThu.Checked = false;
                           ChkThoaithu.Checked = false;
                       }
                
                }
            }

        }

        private void cdPeriodDate_ValueChanged(object sender, EventArgs e)
        {
            Get_Collect();
        }


        private void hiEmployeeSelection1_SelectedEmployeeChanged(object sender, EventArgs e)
        {
            txtNewSalary.EmployeeID = hiEmployeeSelection1.SelectedItem.ID;
            txtOldSalary.EmployeeID = hiEmployeeSelection1.SelectedItem.ID;
        }
    }
}
